import { getCaptcha } from '@/api/user'
import { latestApp } from '@/api/app'

const state = {
  // 手机验证码定时器
  timer: null,
  // 手机验证码倒计时文本
  countDownText: '获取验证码',
  // 图形验证码组件显示隐藏
  showImgDialog: false,
  // 图形验证码
  imgCode: '',
  // 点击确定时触发的方法名
  fnName: '',
  // 点击确定需要的参数
  fnParams: {},
  // 控制小弹窗的显示隐藏的对象
  showLittlePopup: {
    // 聊天窗口上传文件弹窗
    chatSubmitFilePopup: false
  },
  // 原图的属性
  originImage: null,
  // 显示/隐藏查看用户信息的弹框
  ifShowUserInfo: false,
  // 用户信息框类型  1:进行好友验证  2:处理好友申请 3:单纯查看信息
  userInfoType: 0,
  // 显示/隐藏查看个人信息的弹窗
  ifShowPersonalInfo: false,
  // 显示/隐藏视频邀请的弹窗
  ifShowVideoInvitation: false,
  // 显示/隐藏查看原图的模态框
  ifShowOriginDialog: false,
  // 显示/隐藏输入会议密码的弹窗
  ifShowInputPassword: false
}

const mutations = {
  // 设置手机验证码倒计时文本
  set_countDownText (state, time) {
    state.countDownText = time
  },
  // 设置倒计时
  set_timer (state, fn) {
    state.timer = setInterval(fn, 1000)
  },
  // 清除定时器
  clear_timer (state) {
    clearInterval(state.timer)
  },
  // 设置图形验证码组件显示隐藏
  set_showImgDialog (state, boolean) {
    state.showImgDialog = boolean
  },
  // 设置图形验证码
  set_imgCode (state, str) {
    state.imgCode = str
  },
  // 设置触发的方法名
  set_fnName (state, str) {
    state.fnName = str
  },
  // 设置方法需要的参数
  set_fnParams (state, parma) {
    state.fnParams = parma
  },
  // 设置原图框的图片宽高
  set_originImage (state, imageOptions) {
    state.originImage = imageOptions
  },
  // 设置聊天窗口上传文件弹窗的显示
  set_showLittlePopup_chatSubmitFilePopup (state) {
    state.showLittlePopup.chatSubmitFilePopup = !state.showLittlePopup.chatSubmitFilePopup
  },
  // 设置所有小弹窗隐藏
  set_showLittlePopup (state) {
    for (const key in state.showLittlePopup) {
      state.showLittlePopup[key] = false
    }
  },
  // 设置个人信息编辑弹窗的显示隐藏
  set_ifShowPersonalInfo (state, flag) {
    state.ifShowPersonalInfo = flag
  },
  // 设置查看用户信息的弹窗的显示隐藏
  set_ifShowUserInfo (state, flag) {
    state.ifShowUserInfo = flag
  },
  // 设置用户信息框类型
  set_userInfoType (state, type) {
    state.userInfoType = type
  },
  // 设置视频邀请的弹窗的显示隐藏
  set_ifShowVideoInvitation (state, bool) {
    state.ifShowVideoInvitation = bool
  },
  // 设置视频邀请的弹窗的显示隐藏
  set_ifShowOriginDialog (state) {
    state.ifShowOriginDialog = !state.ifShowOriginDialog
  },
  // 设置输入会议密码的弹窗的显示隐藏
  set_ifShowInputPassword (state, bool) {
    state.ifShowInputPassword = bool
  }
}

const getters = {
  // 图形验证码确认方法需要的参数
  params () {
    return {
      ...state.fnParams,
      captcha: state.imgCode
    }
  }
}

const actions = {
  // 获取图形验证码
  getImgCode ({ context }, phone) {
    return new Promise((resolve, reject) => {
      getCaptcha(phone).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 图形验证码弹窗确认按钮功能
  confirm ({ state, dispatch, getters }) {
    return new Promise((resolve, reject
    ) => {
      dispatch(state.fnName, getters.params, { root: true }).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 拉取最新APP
  getLatestApp (context, deviceType) {
    return new Promise((resolve, reject) => {
      latestApp(deviceType).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
